package com.lc.tgxm.util;

import android.content.Context;
import android.os.CountDownTimer;
import android.widget.TextView;

import com.lc.tgxm.R;

/**
 * CountDownTimerService 倒计时封装类
 * Created by Lei on 2018/5/17.
 */
public class CountDownTimerService {
    private static volatile CountDownTimerService instance = null;
    private TextView tvGetVcode;
    private Context context;

    private CountDownTimerService() {
    }

    public static CountDownTimerService getInstance() {
        if (instance == null) {
            synchronized (CountDownTimerService.class) {
                if (instance == null) {
                    instance = new CountDownTimerService();
                }
            }
        }
        return instance;
    }

    private CountDownTimer countTime = new CountDownTimer(GLobalConstant.COUNTDOWN, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            tvGetVcode.setText(millisUntilFinished / 1000 + context.getString(R.string.regetvs));
        }

        @Override
        public void onFinish() {
            tvGetVcode.setText(R.string.regetvcode);
            tvGetVcode.setEnabled(true);
        }

    };

    public CountDownTimerService init(Context context, TextView tvGetVcode) {
        this.context = context;
        this.tvGetVcode = tvGetVcode;
        return this;
    }

    public void startTimer() {
        countTime.start();
        tvGetVcode.setEnabled(false);
    }

    public void cancelTimer() {
        countTime.cancel();
    }

}
